﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Web.Caching;


namespace LanguageR
{
    public static class XMLHelper
    {
        public static string GetLanguageR(HttpContext Context, string key)
        {
            Dictionary<string, string> res = null;
            string filePath = System.Configuration.ConfigurationManager.AppSettings["SMS_String"];
            if (Context.Cache["_LanguageR"] == null)
            {
                filePath = "~/" + filePath;
                string path = Context.Server.MapPath(filePath);
                res = new Dictionary<string, string>();
                XElement xml = XElement.Load(path);
                foreach (var item in xml.Elements())
                    res.Add(item.Attribute("Id").Value,(item.FirstNode==null?"": item.FirstNode.ToString()));

                //缓存依赖的文件路径
                CacheDependency mydep = new CacheDependency(path);
                Context.Cache.Insert("_LanguageR", res, mydep);
            }
            else
            {
                res = Context.Cache["_LanguageR"] as Dictionary<string, string>;
            }
            string val = "";
            res.TryGetValue(key, out val);

            if(string.IsNullOrEmpty(val))
                return "未找到" + key + "所对应的值，请检查" + filePath + "文件！！！";
            else
                return val;
        }
    }
}